package city.spring.controller;

import city.spring.domain.entity.RetreatRegisterEntity;
import city.spring.service.RetreatRegisterService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：退住登记
 *
 * @author 谢金婷
 * @date 2020-04-17 11:33:53
 */
@RestController
@RequestMapping("/retreat_register")
public class RetreatRegisterController extends ApiController {
    private final RetreatRegisterService retreatRegisterService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<RetreatRegisterEntity, ?>> entityOrderFields;

    public RetreatRegisterController(RetreatRegisterService retreatRegisterService) {
        this.retreatRegisterService = retreatRegisterService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(RetreatRegisterEntity::getId);
        entityOrderFields.add(RetreatRegisterEntity::getOldmanId);
        entityOrderFields.add(RetreatRegisterEntity::getIdCard);
        entityOrderFields.add(RetreatRegisterEntity::getRetreatdate);
        entityOrderFields.add(RetreatRegisterEntity::getRegister);
        entityOrderFields.add(RetreatRegisterEntity::getDischargeSituation);
        entityOrderFields.add(RetreatRegisterEntity::getRegisterdate);
        entityOrderFields.add(RetreatRegisterEntity::getEvaluate);
        entityOrderFields.add(RetreatRegisterEntity::getReason);
        entityOrderFields.add(RetreatRegisterEntity::getSettlementTime);
        entityOrderFields.add(RetreatRegisterEntity::getStatus);
    }

    /**
     * 获取全部的 <strong>退住登记</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, RetreatRegisterEntity entity) {
        LambdaQueryChainWrapper<RetreatRegisterEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>退住登记</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, RetreatRegisterEntity entity) {
        LambdaQueryChainWrapper<RetreatRegisterEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<RetreatRegisterEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>退住登记</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(retreatRegisterService.getById(id));
    }

    /**
     * 添加一个 <strong>退住登记</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody RetreatRegisterEntity entity) {
        retreatRegisterService.saveRetreatRegister(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>退住登记</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody RetreatRegisterEntity entity) {
        entity.setId(id);
        retreatRegisterService.updateRetreatRegister(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>退住登记</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        retreatRegisterService.deleteRetreatRegister(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>退住登记</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        retreatRegisterService.deleteRetreatRegister(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<RetreatRegisterEntity> buildLambdaQuery(RetreatRegisterEntity entity) {
        LambdaQueryChainWrapper<RetreatRegisterEntity> lambdaQuery = retreatRegisterService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), RetreatRegisterEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), RetreatRegisterEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getIdCard()), RetreatRegisterEntity::getIdCard, entity.getIdCard());
        lambdaQuery.like(entity.getRetreatdate() != null, RetreatRegisterEntity::getRetreatdate, entity.getRetreatdate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRegister()), RetreatRegisterEntity::getRegister, entity.getRegister());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDischargeSituation()), RetreatRegisterEntity::getDischargeSituation, entity
                .getDischargeSituation());
        lambdaQuery.like(entity.getRegisterdate() != null, RetreatRegisterEntity::getRegisterdate, entity.getRegisterdate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getEvaluate()), RetreatRegisterEntity::getEvaluate, entity.getEvaluate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReason()), RetreatRegisterEntity::getReason, entity.getReason());
        lambdaQuery.like(entity.getSettlementTime() != null, RetreatRegisterEntity::getSettlementTime, entity.getSettlementTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getStatus()), RetreatRegisterEntity::getStatus, entity.getStatus());
        return lambdaQuery;
    }

}
